package com.xiaoxin.controller;

import com.xiaoxin.interceptor.UserHolder;
import com.xiaoxin.model.domain.User;
import com.xiaoxin.model.domain.UserInfo;
import com.xiaoxin.model.vo.PageResult;
import com.xiaoxin.model.vo.UserInfoVo;
import com.xiaoxin.service.UserInfoService;
import com.xiaoxin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021-08-29-21:55
 **/
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /*
     * 查询用户资料
     * token
     * userID
     * */
    @GetMapping
    public ResponseEntity users(Long userID) {
        // @RequestHeader("Authorization") String token
        if (userID == null) {
            userID = UserHolder.getId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /*
     * 更新用户资料
     * */
    @PutMapping
    public ResponseEntity put(@RequestBody UserInfo userInfo) {
        // @RequestHeader("Authorization") String token
        // 设置id
        userInfo.setId(UserHolder.getId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }


    /*
     * 发送验证码
     * */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String mobile = UserHolder.getMobile();
        userService.changeMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /*
     * 校验更换手机验证码
     * */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String mobile = UserHolder.getMobile();
        String code = (String) map.get("verificationCode");
        Map<String, Boolean> retMap = new HashMap<>();
        boolean bb = userService.checkCode(mobile, code);
        retMap.put("verification", bb);
        return ResponseEntity.ok(retMap);
    }

    /*
     * 保存新增的手机号码
     * */
    @PostMapping("/phone")
    public ResponseEntity save(@RequestBody Map map) {
        User user = UserHolder.get();
        String phone = (String) map.get("phone");
        userService.savePhone(user, phone);
        return ResponseEntity.ok(null);
    }

    /*
     * 更换用户头像
     * */
    @PostMapping("/header")
    public ResponseEntity update(MultipartFile headPhoto) throws IOException {
        // @RequestHeader("Authorization") String token
        // 调用service
        userInfoService.updateHead(headPhoto, UserHolder.getId());
        return ResponseEntity.ok(null);
    }


    /*
     * 互相喜欢，喜欢，粉丝 - 统计
     * */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map<String, Integer> map = userInfoService.counts();
        return ResponseEntity.ok(map);
    }


    /*
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * */
    @GetMapping("/friends/{type}")
    public PageResult FriendsVoCounts(@PathVariable("type") String type,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "8") Integer pagesize) {
        return userInfoService.FriendsVoCounts(page,pagesize,type);
    }

}
